]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
Hash: make .size() constant time
authorGiovanni Campagna <gcampagna@src.gnome.org>
Sun, 12 May 2013 18:13:08 +0000 (20:13 +0200)
committerGiovanni Campagna <gcampagna@src.gnome.org>
Sun, 12 May 2013 19:06:34 +0000 (21:06 +0200)
MessageTray calls .size() very often to update the no messages label,
so a linear time implementation is not good enough.

https://bugzilla.gnome.org/show_bug.cgi?id=700194

js/misc/hash.js

index 19d4df619225cc1af7fbe82972ecfa1e61fec554..db70d65427d4a9ce2cf48c7a2c04d14bec31c0c6 100644 (file)
@@ -58,6 +58,7 @@ const Map = new Lang.Class({
 
     _init: function(iterable) {
         this._pool = { };
+        this._size = 0;
 
         if (iterable) {
             for (let i = 0; i < iterable.length; i++) {
@@ -99,6 +100,7 @@ const Map = new Lang.Class({
             node.value = value;
         } else {
             this._pool[hash] = { key: key, value: value };
+            this._size++;
         }
     },
 
@@ -108,6 +110,7 @@ const Map = new Lang.Class({
 
         if (node && _sameValue(node.key, key)) {
             delete this._pool[hash];
+            this._size--;
             return [node.key, node.value];
         } else {
             return [null, null];
@@ -136,6 +139,6 @@ const Map = new Lang.Class({
     },
 
     size: function() {
-        return Object.getOwnPropertyNames(this._pool).length;
+        return this._size;
     },
 });